package com.csii.activiti.server.core.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * swagger配置
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class ActivitiSwagger2Config {

    @Bean
    @ConditionalOnMissingBean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("工作流")
                // 用来创建该API的基本信息，展示在文档的页面中（自定义展示的信息）
                .apiInfo(apiInfo())
                // 设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有有注解的api，用这种方式更灵活
                .apis(RequestHandlerSelectors.basePackage("com.csii"))
                //.apis(RequestHandlerSelectors.basePackage("com.hb.financial"))
                .apis(new Predicate<RequestHandler>() {
                    @Override
                    public boolean apply(@Nullable RequestHandler requestHandler) {
                        //断言
                        assert requestHandler != null;
                        return requestHandler.findAnnotation(ResponseBody.class).isPresent() || requestHandler.findControllerAnnotation(ResponseBody.class).isPresent() || requestHandler.findControllerAnnotation(RestController.class).isPresent();
                    }
                })
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                //.securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()))
                ;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置标题
                .title("API文档")
                //作者信息
                //.contact(new Contact(v2Config.getName(), null, V2Config.getEmail_account()))
                //版本
                .version("版本号:1.0")
                .build();
    }
}
